package com.cloud.school.bus.teacherhelper.modules.stuclass;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.android.support.jhf.handlerui.HandlerToastUI;
import com.cloud.school.bus.teacherhelper.R;
import com.cloud.school.bus.teacherhelper.base.activity.BaseActivity;
import com.cloud.school.bus.teacherhelper.base.view.TitleNavBarView;
/**
 * 修改昵称或者是姓名，flag ：0昵称，1姓名
 * @author lenovo
 *
 */
public class ModifyNickNameActivity extends BaseActivity {

	private static final int NICKNAME_LENGHT = 12;
	
	private EditText mNickNameEditText;
	private TextView mCharCountTextView;
	/**0昵称，1姓名*/
	private int mFlag = 0;
	private String mTitleString;
	private String mHintString;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_modify_nickname);
		
		init();
	}
	
	@Override
	protected void init() {
		
		mFlag = getIntent().getIntExtra("flag", 0);
		if(0 == mFlag){
//			mTitleString = "修改昵称";
			mTitleString = getString(R.string.modify_student_nickname);
		}else if (1 == mFlag) {
//			mTitleString = "修改姓名";
			mTitleString = getString(R.string.modify_student_name);
		}
		mHintString = getIntent().getStringExtra("hint");
		mNickNameEditText = (EditText)findViewById(R.id.nickNameEditText);
		mNickNameEditText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(NICKNAME_LENGHT)});
		mNickNameEditText.setHint(mHintString);
		mCharCountTextView = (TextView)findViewById(R.id.charCountTextView);
		
		setTitleNavBar();
		
		setListener();
	}
	
	@Override
	protected void setListener() {
		mNickNameEditText.setFilters(new InputFilter[] { new InputFilter() {

			@Override
			public CharSequence filter(CharSequence source, int start, int end,
					Spanned dest, int dstart, int dend) {

				if (source.equals("\n")) {
					return "";
				}
				return source;
			}

		} });
		mNickNameEditText.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
				mCharCountTextView.setText(arg0.length() + "/" + NICKNAME_LENGHT);
			}
			
			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				
			}
			
			@Override
			public void afterTextChanged(Editable arg0) {
				
			}
		});
	}
	
	@Override
	protected void setTitleNavBar() {
		TitleNavBarView titleNavBarView = (TitleNavBarView) findViewById(R.id.titleNavBarView);
		titleNavBarView.setMessage(mTitleString);
		titleNavBarView.setLeftButtonOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		titleNavBarView.setRightButtonImageString(R.drawable.btn_confirm_xml, "", R.color.text_blue);
		titleNavBarView.setRightButtonOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				nickNameComplete();
			}
		});
	}

	private void nickNameComplete() {
		if(TextUtils.isEmpty(mNickNameEditText.getText().toString())){
			HandlerToastUI.getHandlerToastUI(mContext, "请填写昵称");
			return;
		}
		Intent intent = new Intent();
		intent.putExtra("name", mNickNameEditText.getText().toString());
		intent.putExtra("flag", mFlag);
		setResult(RESULT_OK, intent);
		finish();
	}
	
}
